import { Injectable } from '@angular/core';
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { _HttpClient } from '@delon/theme';
import { Observable } from 'rxjs';
import { environment } from '@env/environment';

@Injectable()
export class ApiService {

  constructor(private http: _HttpClient) { }

  /**
   * `get` 请求
   * @param url URL地址
   * @param params 请求参数
   * @param options 选项
   */
  get(url: string, params?: any, options?: {
    headers?: HttpHeaders | {
      [header: string]: string | string[];
    };
    observe?: 'body' | 'events' | 'response';
    reportProgress?: boolean;
    responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
    withCredentials?: boolean;
  }): Observable<any> {
    return this.http.get(
      environment.apiUrl + url,
      params,
      options
    );
  }

  /**
   * `post` 请求
   * @param url URL地址
   * @param body 请求body
   * @param params 请求参数
   * @param options 选项
   */
  post(url: string, body?: any, params?: any, options?: {
    headers?: HttpHeaders | {
      [header: string]: string | string[];
    };
    observe?: 'body' | 'events' | 'response';
    reportProgress?: boolean;
    responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
    withCredentials?: boolean;
  }): Observable<any> {
    return this.http.post(
      environment.apiUrl + url,
      body,
      params,
      options
    );
  }

  /**
   * `delete` 请求
   * @param url URL地址
   * @param params 请求参数
   * @param options 选项
   */
  delete(url: string, params?: any, options?: {
    headers?: HttpHeaders | {
      [header: string]: string | string[];
    };
    observe?: 'body' | 'events' | 'response';
    reportProgress?: boolean;
    responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
    withCredentials?: boolean;
  }): Observable<any> {
    return this.http.delete(
      environment.apiUrl + url,
      params,
      options
    );
  }

  /**
   * `jsonp` 请求
   *
   * @param url URL地址
   * @param params 请求参数
   * @param callbackParam CALLBACK值，默认：JSONP_CALLBACK
   */
  jsonp(url: string, params?: any, callbackParam?: string): Observable<any> {
    return this.http.jsonp(
      environment.apiUrl + url,
      params,
      callbackParam
    );
  }

  /**
   * `patch` 请求
   *
   * @param url URL地址
   * @param body 请求参数
   */
  patch(url: string, body?: any, params?: any): Observable<any> {
    return this.http.patch(
      environment.apiUrl + url,
      body,
      params
    );
  }

  /**
   * `put` 请求
   *
   * @param url URL地址
   * @param body 请求参数
   */
  put(url: string, body?: any, params?: any): Observable<any> {
    return this.http.put(
      environment.apiUrl + url,
      body,
      params
    );
  }

  /**
   * `request` 请求
   *
   * @param method 请求方法类型
   * @param url URL地址
   * @param options 参数
   */
  request<R>(method: string, url: string, options?: {
    body?: any;
    headers?: HttpHeaders | {
      [header: string]: string | string[];
    };
    observe?: 'body' | 'events' | 'response';
    params?: HttpParams | {
      [param: string]: string | string[];
    };
    responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
    reportProgress?: boolean;
    withCredentials?: boolean;
  }): Observable<R> {
    return this.http.request(
      method,
      environment.apiUrl + url,
      options
    );
  }

}
